package com.tianchi.lexiang.background;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RecoverySystem;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.tianchi.lexiang.R;
import com.tianchi.lexiang.background.util.PublicUtil;
import com.tianchi.lexiang.bean.LeXiangConstants;
import com.tianchi.lexiang.util.FtpUnit;

public class UpdatedVersionInformation extends Activity implements OnItemClickListener {

	private static final String remoteFileName = LeXiangConstants.REMOTE_FILE_NAME;
	private static final String localFileName = LeXiangConstants.LOCAL_FILE_NAME;
	private static final String TAG = "UpdatedVersionInformation";
	
	private FtpUnit ftpUnit = FtpUnit.getInstance();
	private PublicUtil publicUtil = PublicUtil.getPulibUtil();
	private ProgressDialog progressDialog;
	private ListView backgroundSettingList;
	private Button backBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.background_setting);
		initView();
	}

	private void initView() {
		backgroundSettingList = (ListView) findViewById(R.id.background_setting_list);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.getResources().getStringArray(R.array.update_program));
		backgroundSettingList.setAdapter(adapter);
		backgroundSettingList.setOnItemClickListener(this);
		backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				UpdatedVersionInformation.this.finish();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		switch (position) {
		case 0:
			updateFromUdisk();
			break;
		case 1:
			updateFromWebDialog();
			break;
		case 2:
			Intent intent = new Intent();
			intent.setClass(UpdatedVersionInformation.this, HardDiskSizeInfo.class);
			this.startActivity(intent);
			break;
		default:
			break;
		}
	}

	private void updateFromWebDialog() {
		AlertDialog.Builder builder = new Builder(
				UpdatedVersionInformation.this);
		builder.setMessage(getString(R.string.update_message));
		builder.setTitle(getString(R.string.update_tip));
		builder.setPositiveButton(getString(R.string.update_now), new Dialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				progressDialog = ProgressDialog.show(UpdatedVersionInformation.this, getString(R.string.are_download),
						getString(R.string.download_in), true);
				new DownLoadTask().execute();
			}
		});
		builder.setNegativeButton(getString(R.string.update_later), new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	private void updateFromUdisk(){
		AlertDialog.Builder builder = new Builder(
				UpdatedVersionInformation.this);
		builder.setMessage(getString(R.string.update_message));
		builder.setTitle(getString(R.string.update_tip));
		builder.setPositiveButton(getString(R.string.update_now), new Dialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				File file=new File(LeXiangConstants.UDISK_FILE_PATH);
				if(file.exists()){
					boolean bool = publicUtil.copyZip(LeXiangConstants.UDISK_FILE_PATH, LeXiangConstants.CACHE_PATH);
					if(bool){
						File updateFile = new File(LeXiangConstants.LOCAL_FILE_NAME);
						promptRebootDialog(updateFile);
					}
				}else{
					dialog(getString(R.string.udisk_update_error));
				}
			}
		});
		builder.setNegativeButton(getString(R.string.update_later), new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	class DownLoadTask extends AsyncTask<String, Integer, Boolean> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Boolean doInBackground(String... arg0) {
				if (ftpUnit.loadFile(remoteFileName, localFileName)) {
					return true;
				} else {
					return false;
				}
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if (result) {
				progressDialog.dismiss();
				promptRebootDialog(new File(localFileName));
			} else{
				progressDialog.dismiss();
				dialog(getString(R.string.web_update_error));
			}
			ftpUnit.closeConnect();
		}
	}
		
	private void dialog(String tip) {
		AlertDialog.Builder builder = new Builder(
				UpdatedVersionInformation.this);
		builder.setMessage(tip);
		builder.setIcon(android.R.drawable.ic_menu_info_details);
		builder.setTitle(getString(R.string.tip));
		builder.setPositiveButton(getString(R.string.confirm),
				new Dialog.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}
	
	  private void promptRebootDialog(final File updateFile){
		  	Log.d(TAG, "[promptRebootDialog]....reboot");
	    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    	builder.setTitle(R.string.reboot_prompt_title);
	    	builder.setMessage(R.string.reboot_prompt_message);
	    	builder.setPositiveButton(R.string.confirm_reboot, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					recoverSystem(updateFile);
				}
			});
	    	builder.setNegativeButton(R.string.cancel,  new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
						File updateFile = new File(LeXiangConstants.LOCAL_FILE_NAME);
						if(updateFile.exists()){
							updateFile.delete();
						}
						dialog.dismiss();
				}
	    		
	    	});
	    	builder.create();
	    	builder.show();
	    }
	    
	private void recoverSystem(File file) {
		try {
			RecoverySystem.installPackage(UpdatedVersionInformation.this, file);
		} catch (IOException e) {
			Log.e(TAG, "recover system fail");
			e.printStackTrace();
		}
	}
}
